/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo360.mobilesafe.clearsdkwrapper;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import com.qihoo360.mobilesafe.utils.DexUtils;
import com.qihoo360.mobilesafe.utils.SharedPref;
import com.qihoo360.mobilesafe.utils.ZipUpdate;
import dalvik.system.DexClassLoader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class ProcessClearWrapper {
    private static Context mContext;
    private static final String TAG = "ProcessClearWrapper";
    private static boolean DEBUG = false;
    private static final String CLEAR_APPSPEED_SHARE_PATH = "o_c_speed_s_p";

    public ProcessClearWrapper(Context paramContext) {
        mContext = paramContext;
    }

    public void boot() {
        if (DEBUG)
            Log.d("ProcessClearWrapper", "boot()");
        if (checkSharedFile())
            return;
        check();
        doClear();
    }

    public void check() {
        long l1 = SharedPref.getGzipVersion(mContext, "libclearsdk.gzip");
        long l2 = SharedPref.getBundleVersion(mContext, "libclearsdk.gzip");
        long l3 = SharedPref.getGzipVersion(mContext, "libclearsdk_new.gzip");
        if ((l2 > l1) && (l2 > l3)) {
            new ZipUpdate(mContext).copyAssetToFile();
            if (!(DEBUG))
                return;
            Log.d("ProcessClearWrapper", "check,copyAssetToFile");
        }
        else {
            if ((l3 <= l1) || (l3 <= l2))
                return;
            new ZipUpdate(mContext).copyNewToFile();
            if (!(DEBUG))
                return;
            Log.d("ProcessClearWrapper", "check,copyNewToFile");
        }
    }

    public void doClear() {
        try {
            DexClassLoader localDexClassLoader = DexUtils.getDexClassLoader(mContext, "libclearsdk" + File.separator
                    + "libcleardex.jar", "libclearsdk" + File.separator + "libcleardex.dex");
            Object localObject = DexUtils.get_class_new(DexUtils.get_class_contructor(localDexClassLoader,
                    "com.qihoo360.mobilesafe.opti.processclear.ProcessClearExecutor", new Class[] { Context.class }),
                    new Object[] { mContext });
            DexUtils.get_class_object(localObject, DexUtils.get_class_method(localDexClassLoader,
                    "com.qihoo360.mobilesafe.opti.processclear.ProcessClearExecutor", "doClear", new Class[0]),
                    new Object[0]);
        } catch (Throwable localThrowable) {}
    }

    private boolean checkSharedFile() {
        if (DEBUG)
            Log.d("ProcessClearWrapper", "sdk checkSharedFile");
        if ("mounted".equals(Environment.getExternalStorageState())) {
            File localFile1 = Environment.getExternalStorageDirectory();
            if (localFile1 != null) {
                BufferedReader localBufferedReader = null;
                try {
                    String str1 = pathAppend(localFile1.getAbsolutePath(), "o_c_speed_s_p");
                    File localFile2 = new File(str1);
                    if ((localFile2 == null) || (!(localFile2.exists()))) {
                        if (DEBUG)
                            Log.d("ProcessClearWrapper", "sdk checkSharedFile not exist!");
                        return false;
                    }
                    localBufferedReader = new BufferedReader(new FileReader(str1), 1024);
                    String str2;
                    while (!(TextUtils.isEmpty(str2 = localBufferedReader.readLine()))) {
                        if (DEBUG)
                            Log.d("ProcessClearWrapper", "sdk checkSharedFile, line: " + str2);
                        String[] arrayOfString = str2.split("\\=");
                        if (arrayOfString == null)
                            continue;
                        String str3 = arrayOfString[0];
                        String str4 = arrayOfString[1];
                        if (!(mContext.getPackageName().equals(str3)))
                            continue;
                        if (!(isPkgInstalled(mContext, str4)))
                            break label228;
                        label228: return true;
                    }
                } catch (Exception localException1) {
                    if (DEBUG)
                        localException1.printStackTrace();
                } finally {
                    try {
                        if (localBufferedReader != null)
                            localBufferedReader.close();
                    } catch (Exception localException5) {}
                }
            }
        }
        return false;
    }

    private static String pathAppend(String paramString1, String paramString2) {
        if (paramString1 == null)
            paramString1 = "";
        if (paramString2 == null)
            paramString2 = "";
        StringBuffer localStringBuffer = new StringBuffer(paramString1);
        if (!(paramString1.endsWith("/")))
            localStringBuffer.append('/');
        localStringBuffer.append(paramString2);
        return localStringBuffer.toString();
    }

    private static boolean isPkgInstalled(Context paramContext, String paramString) {
        PackageManager localPackageManager = paramContext.getPackageManager();
        if (localPackageManager == null)
            return false;
        PackageInfo localPackageInfo = null;
        try {
            localPackageInfo = paramContext.getPackageManager().getPackageInfo(paramString, 0);
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {} catch (Exception localException) {
            localException.printStackTrace();
        }
        return (localPackageInfo != null);
    }
}